# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is mozilla.org code.
#
# Contributor(s):
#   Chris Jones <jones.chris.g@gmail.com>
#
# Alternatively, the contents of this file may be used under the terms of
# either of the GNU General Public License Version 2 or later (the "GPL"),
# or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****

__all__ = [ 'parse', 'genrewriter' ]

import os, sys

from porky.parser import yacc as _yacc
import porky.config as _config


def _getcallerpath():
    '''Return the absolute path of the file containing the code that
**CALLED** this function.'''
    return sys._getframe(1).f_code.co_filename


def parse(specstring):
    '''Parses |specstring| and returns a porky AST if successful.
This AST object can be passed to |genrewriter()| and |genmakefile()|.'''
    return _yacc.parse(input=specstring, debug=1)


def genrewriter(ast, _outdir):
    '''Generate a Pork rewriter from |ast|, a Makefile that can be used
to compile it, and a convenience script to invoke the rewriter.
Save all to |outdir|.'''
    # need the module's directory so we can locate templates and files that
    # will be symlinked
    thisdir, _ = os.path.split(_getcallerpath())

    outdir = _outdir; i = 0
    while os.path.lexists(outdir): i += 1; outdir = _outdir +'.'+ str(i)
    os.mkdir(outdir)
    ast.gencode(open(os.path.join(outdir, 'rules.gen.h'), 'w'))

    open(os.path.join(outdir, 'config.gen.mk'), 'w').write(
'''# Automatically generated by porkyc.  Edit at your own risk.
include %s
include %s
REWRITER = %s
''' %(os.path.join(_config.PIGLET_DIR, 'config.mk'),
      os.path.join(_config.PIGLET_DIR, 'build.mk'),
      ast.name))

    # our ./configure provides us with the pork-barrel dir.
    rewritescript = os.path.join(outdir, 'dorewrite')
    open(rewritescript, 'w').write('''#!/bin/bash
# Automatically generated by porkyc.  Edit at your own risk.
export PYTHONPATH="%s:${PYTHONPATH}"
exec python -m pork_barrel '%s %%(file)s' "$@"
'''% (_config.PIGLET_DIR,
      os.path.join(os.path.abspath(outdir), ast.name)))
    os.chmod(rewritescript, 0755)

    os.symlink(os.path.join(thisdir, 'porky.link.cc'), 
               os.path.join(outdir, ast.name + '.cc'))
    os.symlink(os.path.join(thisdir, 'Makefile.link.mk'),
               os.path.join(outdir, 'Makefile'))

    return outdir
